//
//  File.swift
//
//
//  Created by didi on 2022/9/9.
//

import Foundation

public extension Encodable {
    /// transforms an encodable object into another decodable if possible.
    func transform<U: Decodable>(to type: U.Type) throws -> U {
        let encoder = JSONEncoder()
        let data = try encoder.encode(self)
        let decoder = JSONDecoder()
        return try decoder.decode(type, from: data)
    }
}
